package com.jcgyl.export.service.Impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jcgyl.export.dao.CustomerDao;
import com.jcgyl.export.entity.Customer;
import com.jcgyl.export.entity.Tbsysuser;
import com.jcgyl.export.mapper.CustomerMapper;
import com.jcgyl.export.service.CustomerService;
import com.jcgyl.export.util.entity.Result;

@Service
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private CustomerDao customerDao;

	@Override
	public Map<String, Object> findAllCustomer(String name, String code, String contact, String phone, String email,
			int pageIndex, int pageSize) {
		// 总行数
		int rowCount =customerDao.findRowCount(name, code, contact, phone, email);
		int startIndex = (pageIndex - 1) * pageSize;
		List<Customer> list = customerDao.findAllCustomer(name, code, contact, phone, email, startIndex, pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", list);
		map.put("totalRows", rowCount);
		return map;
	}

	@Override
	public int findRowCount(String name, String code, String contacts, String tel, String email) {
		int rowCount = customerDao.findRowCount(name, code, contacts, tel, email);
		return rowCount;
	}

	@Override
	public Customer findByNameOrCode(String name, String code) {
		Customer c = customerDao.findByNameOrCode(name, code);
		return c;
	}

	@Override
	public List<Customer> findByAddDate(Date date) {
		return null;
	}

	/** 添加客户 */
	@Override
	public Result addCustomer(Customer customer) {
        Tbsysuser user=(Tbsysuser)SecurityUtils.getSubject().getSession().getAttribute("user");
		try {
			Customer c = customerDao.findByNameOrCode(customer.getName(), customer.getCiqCode());
			if (c != null)
				Result.build(1, "姓名或代码已存在！");
			customer.setIsDelete(0);
			customer.setValid(0);
			customer.setAddName(user.getUname());
			customer.setCreatedTime(new Date());
			customerMapper.insertSelective(customer);
			return Result.ok("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getCause().getMessage());
		}
	}

	/** 修改客户 */
	@Override
	public Result updateCustomer(Customer customer) {
		Tbsysuser user=(Tbsysuser)SecurityUtils.getSubject().getSession().getAttribute("user");
		try {		
			Customer customer_update=customerDao.findById(customer.getCustomerId());
			customer_update.setName(customer.getName());
			customer_update.setCode(customer.getCode());
			customer_update.setCiqCode(customer.getCiqCode());
			customer_update.setCrNumber(customer.getCrNumber());
			customer_update.setContact(customer.getContact());
			customer_update.setPhone(customer.getPhone());
			customer_update.setEmail(customer.getEmail());
			customer_update.setAddress(customer.getAddress());
			customer_update.setCustomerType(customer.getCustomerType());
			customer_update.setDefaultLogistics(customer.getDefaultLogistics());
			customer_update.setValid(customer.getValid());
			customer_update.setNote(customer.getNote());
			customer_update.setModifiedName(user.getUname());
			customer_update.setModifiedTime(new Date());
			customerMapper.updateByPrimaryKey(customer_update);
			System.out.println("修改成功！");
			return Result.ok("修改成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getCause().getMessage());
		}
	}

	/** 改变客户的状态 */
	@Override
	public Result changeState(int customerId, int isDelete) {
		try {
			if(isDelete==0){
			    isDelete=1;
			}else{
				isDelete=0;
			}
			customerDao.changeState(customerId, isDelete);
			return Result.ok("更改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getCause().getMessage());
		}
	}

	/** 删除客户 */
	@Override
	public Result deleteCustomer(int customerId) {
		try {
			customerMapper.deleteByPrimaryKey(customerId);
			return Result.ok("删除成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getCause().getMessage());
		}
	}

}
